﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TWAB55DI
    {

        public TWAB55DI()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MESSAGES += "DIABPNEW;";
            MESSAGES += "LOADABP;RESETPROD;";
            InitializeComponent();
            //POSTYP = "DI ";
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwRule != null)
            {
                VKDELDATE = dwRule["P7"].ToString();
            }
        }

        #region====WorkFlow========
        MessageInfo Lastmsg
        {
            get
            {
                return TMV.Lastmsg;
            }
            set
            {
                TMV.Lastmsg = value;
            }
        } 
        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            IsBusy = true;
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "DIABPNEW":
                    if (Lastmsg != null)
                    {
                        MessageBox.Show("请先完成前一个订单创建");
                        //return;
                    }
                    RULEKEY = "";//先装载规则
                    RULEKEY = msg.ORDER;
                    Lastmsg = msg;
                    ARTST = msg.WAKZ;
                    ART = msg.ART;
                    VAR = msg.VAR;
                    FARBE = msg.FARBE;
                    ART = msg.ART;
                    VAR = msg.VAR;

                    DESS = msg.DESS;
                    COLORIT = msg.COLORIT;
                    FSIZE = msg.GROE;
                    FINISH = msg.BESCHICH;
                    PRART = ARTST + ART + VAR + FARBE;
                    PM["ORDER"] = msg.ORDER;
                    PM["WAKZ"] = msg.WAKZ;
                    PM["ART"] = msg.ART;
                    PM["VAR"] = msg.VAR;
                    PM["FARBE"] = msg.FARBE;
                    PM["BESCHICH"] = msg.BESCHICH.PadRight(10);
                    PM["DESS"] = msg.DESS.PadRight(10);
                    PM["COLORIT"] = msg.COLORIT.PadRight(10);
                    PM["GROE"] = msg.GROE.PadRight(7);
                    PM["MAKE2ORD"] = MAKE2ORD;
                    IsOK = false;
                    TMV.DIABPNEW(PM, ref IsOK);
                    IsABPCreated = IsOK;
                    if (IsABPCreated)
                    {
                        ORDERNUM = msg.ORDER;
                    }
                    IsShowAB = false;
                    IsShowAB = true;
                    break;
            }
            IsBusy = false;
        }
        #endregion

        public override void OnAutoChg(bool auto)
        {
            if (!auto)
                return;
            if (dwRule == null)
            {
            }
            base.OnAutoChg(auto);
        }
        public override void LOAD(string _order)
        {
            base.LOAD(_order);
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (!IsCanLoad)
                return;
            if (EXAUF.Length < 11)
                return;
            ORDERNUM = "";
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 6);
            POS = EXAUF.Substring(8, 3);
            ORDERNUM = KRS + LFD + POS + POSTYP;
            base.OnEXAUFChg(vlaue);
        }
        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            base.LOADBYAUF();
        }
        public override void OnFLOWChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (FLOW == "")
                return;
            TMV.ABPANRKR = FLOW.Substring(0, 2);
            TMV.ABPALFDNR = FLOW.Substring(2, 6);
            TMV.UABP19(dwMaster);
            PM["ORDERNUM"] = TMV.ABPAUFKRS + TMV.ABPAUFLFD + TMV.ABPAUFPOS;
            PM["AUFTYP"] = "AB55";
            TMV.VABF.ABFNEW(PM);
            TMV.VPR.UpdateICC_PRULES(dwRule, 1);
            string key = KEY;
            KEY = "";
            KEY = key;
            base.OnFLOWChg(value);
        }
        private void btnPrePlan_Click(object sender, RoutedEventArgs e)
        {
            try
            {


                IsABPCreated = false;
                if (Lastmsg == null)
                {
                    Lastmsg = new MessageInfo();
                    Lastmsg.ORDER = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString();
                    Lastmsg.WAKZ = dwAB["ABPWAKZ"].ToString();
                    Lastmsg.ART = dwAB["ABPART"].ToString();
                    Lastmsg.VAR = dwAB["ABPVAR"].ToString();
                    Lastmsg.FARBE = dwAB["ABPFARBE"].ToString();
                    Lastmsg.BESCHICH = dwAB["ABPBESCHICH"].ToString();
                    Lastmsg.DESS = dwAB["ABPDESS"].ToString();
                    Lastmsg.COLORIT = dwAB["ABPCOLORIT"].ToString();
                    Lastmsg.GROE = dwAB["ABPGROE"].ToString();
                }
                PM["ORDER"] = Lastmsg.ORDER;
                PM["WAKZ"] = Lastmsg.WAKZ;
                PM["ART"] = Lastmsg.ART;
                PM["VAR"] = Lastmsg.VAR;
                PM["FARBE"] = Lastmsg.FARBE;
                PM["BESCHICH"] = Lastmsg.BESCHICH.PadRight(10);
                PM["DESS"] = Lastmsg.DESS.PadRight(10);
                PM["COLORIT"] = Lastmsg.COLORIT.PadRight(10);
                PM["GROE"] = Lastmsg.GROE.PadRight(7);
                PM["MAKE2ORD"] = MAKE2ORD;
                IsOK = false;
                TMV.DIABPNEW(PM, ref IsOK);
                IsABPCreated = IsOK;
                if (IsABPCreated && dwAB != null)
                {
                    string key = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
                    ORDERNUM = "";
                    ORDERNUM = key;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
    partial class VABPOS
    {
        public void DIABPNEW(Dictionary<string, object> pm, ref bool IsABPCreated)
        {
            try
            {
                if (dwRule["UNDES"].ToString().Trim().Length < 8)
                    return;
                if (PRLO.Length < 2)
                    return;
                if (PRLB.Length < 2)
                    return;
                if (dwWS == null)
                {

                }
                PM["VKAUF"] = dwRule["P4"];
                PM["KRS"] = KRS;
                APZANRKRS = dwRule["UNDES"].ToString().Substring(0, 2);
                APZALFDNR = dwRule["UNDES"].ToString().Substring(2, 6);
                if (APZALFDNR.Trim().Length < 6)
                {
                    throw new Exception("请选择流程");
                }
                #region======Rules======== 
                INMODE = dwRule["P8"].ToString();
                OUTMODE = dwRule["P9"].ToString();
                double _R01 = 0;
                double ROLLS = 0;
                double _LEADTIME = 0;
                if (dwRule != null)
                {
                    _R01 = R01 / 100;
                    ROLLS = double.Parse(dwRule["UNITNUM"].ToString());
                    _LEADTIME = Convert.ToDouble(_LEADTIME);//生产周期
                }
                double WARSTDLG = Convert.ToDouble(dwWS["WARSTDLG"].ToString());
                double WAFBRTL = System.Convert.ToDouble(dwWS["WAFBRTL"].ToString());
                double WARGRLFD = System.Convert.ToDouble(dwWS["WARGRLFD"].ToString());
                double WAFGRLFD = System.Convert.ToDouble(dwWS["WAFGRLFD"].ToString());
                double WARWBRT = System.Convert.ToDouble(dwWS["WARWBRT"].ToString());
                if (WARSTDLG == 0)
                {
                    WARSTDLG = 100000;
                }
                if (ROLLS == 0)
                {
                    ROLLS = Math.Ceiling(0.01 * QTYEND * 100 / (WARSTDLG * 0.001));//无放大系数  
                }
                string STARTD = Helpers.Helper.GetDateAddDays(DELDATE, Convert.ToInt16(0 - _LEADTIME));
                if (STARTD.Trim() == "") STARTD = "0";
                #endregion
                #region=======dwData==========
                DataRow dwABP = TBMain.NewRow();
                SetUpNewRow(ref dwABP, null);
                ShareedABP(dwABP, pm);
                dwABP["ABPAUFART"] = "DI";
                dwABP["ABPZLO"] = PRLO;
                dwABP["ABPAENDZL"] = 5;
                dwABP["ABPFA"] = 0;
                dwABP["ABPGA"] = 0;
                dwABP["ABPQA"] = 0;
                dwABP["ABPZLO"] = PRLO;
                dwABP["ABPGESSTA"] = "TE";
                dwABP["ABPWBSTA"] = "60";
                dwABP["ABPAESTAT"] = "J";
                dwABP["ABPSERIE"] = " ";
                dwABP["ABPKETTAUFT"] = "N";
                dwABP["ABPMGV"] = 0;
                dwABP["ABPMGB"] = 0;
                dwABP["ABPTMCOUNT"] = 3;
                dwABP["ABPREGEL"] = "00000000";

                dwABP["ABPSTDLAE"] = 0;
                dwABP["ABPDIVARTKZ"] = " ";
                dwABP["ABPSOLLMENGE1"] = QTYEND * 100;//10
                dwABP["ABPSOLLMENGE2"] = QTYEND * 100;
                dwABP["ABPSOLLMENGE3"] = QTYEND * 100 * WARWBRT * 0.001;
                dwABP["ABPSOLLMENGE4"] = QTYEND * 100 * WAFBRTL * 0.001;
                dwABP["ABPSOLLMENGE5"] = QTYEND * 100 * WARGRLFD * 0.001;
                dwABP["ABPSOLLMENGE6"] = QTYEND * 100 * WAFGRLFD * 0.001;
                dwABP["ABPSOLLMENGE7"] = "0";
                dwABP["ABPSOLLMENGE8"] = ROLLS * 100;

                dwABP["ABPANZSTK"] = ROLLS;
                dwABP["ABPZUGETGES"] = dwABP["ABPSOLLMENGE1"];
                dwABP["ABPSTKLG"] = "0";
                dwABP["ABPSZUSATZKZ1"] = " ";

                dwABP["ABPSME"] = 1;
                string vkp = dwRule["P5"].ToString().PadRight(11).Substring(0, 11);
                dwABP["ABPKDPARTIE"] = vkp.Substring(0, 2) + vkp.Substring(4, 4) + "-" + vkp.Substring(8, 3);//销售单号
                #endregion
                Insert(dwABP);
                IsABPCreated = true;
                VPR.UpdateICC_PRULES(dwRule, 1);

                PreparePM(dwABP);
                PM["MAKE2ORD"] = pm["MAKE2ORD"];
                VPAR.NABPR(PM);

                VABF.ABFNEW(PM);
                PM["WAKZ"] = pm["WAKZ"];
                PM["VKAUF"] = dwRule["P4"];
                PM["VKPAUF"] = dwRule["P5"];
                PM["ABAAUFTYP"] = "V";
                VMU.MUNEW(PM);

                PM["WAKZ"] = "R";
                PM["WDTYP"] = "Z";
                PM["INMODE"] = INMODE;
                PM["OUTMODE"] = OUTMODE;
                VWD.NEWWDZ(PM);
                if (JNUM == "0")
                {
                    PM["JNUM"] = "1";
                }
                else
                {
                    PM["JNUM"] = JNUM;//织轴个数
                } 
                PM["R01"] = _R01;
                PM["JWASTGE"] = JWASTGE;//每个织轴的损耗
                PM["KETTART"] = UMS.PadRight(6);
                PM["MAKE2ORD"] = pm["MAKE2ORD"];
                PM["FARBE"] = pm["FARBE"];
                VBD.NEWBD723(PM);

                VGD.NEWSGD723(PM);

                Lastmsg = null;

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.Message);
            }
            finally
            {

            }
        }
    }

}
